//header js
$(document).ready(function() {
	// 7.31版本暂不支持英文版，若URL中含有“/en/”，则转化为“/sc/”。
	var regionCode = getRegionCode();//地区代码
	var curURL = document.location.href;
	var isHttp = false;
	if (curURL.indexOf("http://") == 0) {
		curURL = curURL.replace("http://","https://");
		isHttp = true;
	}
	if (isHttp == true) {
		//if(!(curURL.indexOf("/sfbuy/sc/")>0))
		document.location.href = curURL;
	 }
	 
	 if("cn"!=getRegionCode()){
	 	$("#newVersionBtn").hide();
	 }else{
	 	$("#newVersionBtn").show();
	 }
	 
	 //控制banner控制，首页之外不显示
	if (!curURL.endWith("/index.html") && !curURL.endWith("/login.html") && !curURL.endWith("/register.html")&& !curURL.endWith("/cn/sc/") ) {
		$("#new_version_exp_banner").hide();
	}else{
		//体验新版入口
		//登录状态
		if('2' == getL()){
			//个人版
			if( getUT()=='0' && getCT()!='1' && "cn"==getRegionCode()){
				$("#new_version_exp_banner").show();
				$("#new_version_exp_banner").click(function(){
					window.location.href=getCurHostName()+"/new/cn/sc/i-index.html";
				});
			}else{
				$("#new_version_exp_banner").hide();
			}
		}else{
			//非登录状态
			if("cn"!=getRegionCode()){
				$("#new_version_exp_banner").hide();
			}else{
				$("#new_version_exp_banner").show();
				$("#new_version_exp_banner").click(function(){
					window.location.href=getCurHostName()+"/new/cn/sc/index.html";
				});
			}
		}		
	}
	 
	$("#_header_send_normal").parent().removeClass("loginsign");
	$("#warmtips:hidden").show();
	$("#warmtips_hmt:visible").hide();
	if("cn"!=regionCode){
		$("#_header_send_normal").parent().addClass("loginsign");
		$("#warmtips:visible").hide();
		$("#warmtips_hmt:hidden").show();
	}
	if (!checkAuthority()) {
    	return;
    }
    strengthLocation();
	var l = getL();
	
	if (l == 2 ) {
		$("#welcomeMsg").text(i18n.Welcome + getU());
	}
	
	// 自动登录，暂时无效
//	if (l != 2){
//		if (getCookie("c") != undefined) {
//			asyncCallService("/service/mticket", "POST", "json", "", function(data) {
//				if (data.l == 2) {
//					addCookieWithNoExpires("l", data.l);
//					addCookieWithNoExpires("u", data.u);
//					addCookie("c", data.c);
//					$("#mem_logout").css("display","");
//					$("#welcomeMsg").css("display","");
//					$("#welcomeMsg").text(i18n.Welcome + getU());
//					$("#_header_mem_login").css("display","none");
//					$("#_header_mem_register").css("display","none");
//					$("#_index_fastlogin").css("visibility","hidden");
//				} else {
//					delCookie("c");
//				}
//			}, function(data){});			
//		}
//	}
	
	$("#logoutLink").click(function(){
		//syncCallService("/service/returnticket", "POST", "json", "", function(data){
		var host=getCurHostName();
		syncCallService(host+"/sso/logout?service="+host+"/service/ticket/loginOut", "POST", "json", "", function(data){
			delCookie("l");
			delCookie("u");
			delCookie("c");
			delCookie("t");
//			delCookie("JSESSIONID");
			delCookie("ct");
			document.location.href="/" + i18n.name + "/index.html";
		}, function(data){});
	});
	
	$("#_header_mem_login").click(function(){
		login();
	});
	
/*	$("#_header_inner_send_staff_li").hover(function(){
		$(this).addClass("hover").children(".thirdnav").stop(true,true).show("fast");
	},function(){
		$(this).removeClass("hover").children(".thirdnav").stop(true,true).hide();}
	);*/
	
});

function showSendHeader2() {
	$("._header2_send_record").css("display","none");
	$("._header2_account_manager").css("display","none");
}
function showInfoSendHisHeader2() {
	$("._header2_send_record").css("display","");
	$("._header2_account_manager").css("display","none");
}
function showAccountManagerHeader2() {
	$("._header2_send_record").css("display","none");
}
function getCurURI(){
	var curURL = document.location.href;
	var location = document.location;
	var curURI = curURL.substr(location.protocol.length + location.hostname.length + 2);
	if (curURI.indexOf("?")!=-1) {
		curURI = curURI.substr(0, curURI.indexOf("?"));
  	}	
	while (true) {
		if (curURI.substr(0,1) == "/") {
			break;
		} else {
			curURI = curURI.substr(1);
		}
	}
	return curURI;
}
function setMenuCookie(){
	var curURI = getCurURI();
	var hasSetCookie = 0;
	for(var i = 0; i<urlToMenuCookie.length;i++){
		if(inArray(curURI, urlToMenuCookie[i].url)){
			delCookie("p");
			addCookie("p",urlToMenuCookie[i].pName);
			if(urlToMenuCookie[i].p2Name!=""){
				delCookie("p2");
				addCookie("p2",urlToMenuCookie[i].p2Name);
			}
			hasSetCookie = 1;
			break;
		}
	}
	if(hasSetCookie == 0){
		delCookie("p");
		delCookie("p2");
	    addCookie("p","_header_home");
	}
}
function strengthLocation(){
	setMenuCookie();
    $("#sfnav>li").removeClass("cur");
    if(window.location.href.indexOf("index.html") != -1||getCurURI().substr(getCurURI().length-1,1)=="/"){
        $("#_header_home").addClass("cur");;
    } else {
        var p = getCookie("p");
        if(p == undefined){
            $("#_header_home").addClass("cur");
        } else {
            $("#" + p).parent("li").addClass("cur");
        }
        var p2 = getCookie("p2");
        if (p2 != undefined) {
        	$("#" + p2).addClass("cur");
        }
    }
}

function inArray(obj, arr) {
	if (typeof obj == 'string') {
		for ( var i in arr) {
			if (stringContains(obj, arr[i])) {
				return true;
			}
		}
	}
	return false;
}

function stringContains(string, substr/**, isIgnoreCase*/) {
//	if (isIgnoreCase) {
//		string = string.toLowerCase();
//		substr = substr.toLowerCase();
//	}
	var startChar = substr.substring(0, 1);
	var strLen = substr.length;
	for ( var j = 0; j < string.length - strLen + 1; j++) {
		if (string.charAt(j) == startChar)// 如果匹配起始字符,开始查找
		{
			if (string.substring(j, j + strLen) == substr)// 如果从j开始的字符与str匹配，那ok
			{
				return true;
			}
		}
	}
	return false;
}

// 在页面加载完毕后，再次检查登录状态，是否有权限打开该页面（主要预防用户直接输入url访问页面）
function checkAuthority() {
	var l = getL();
	var curURL = document.location.href;
	var location = document.location;
	var curURI = curURL.substr(location. protocol.length + location.hostname.length + 2);
	if (curURI.indexOf("?")!=-1) {
		curURI = curURI.substr(0, curURI.indexOf("?"));
  	}	
	while (true) {
		if (curURI.substr(0,1) == "/") {
			break;
		} else {
			curURI = curURI.substr(1);
		}
	}
	if (l==0) {
		// 判断当前页面是否在needLoginUrl中,
		if (inArray(curURI, needLoginUrl)||inArray(curURI, needInnerUrl)||inArray(curURI, needMonthUrl)) {
			document.location.href = "/" + i18n.name + "/login.html?" + "url=" + curURI;
			return false;
		}
	}
	if (getUT() == 1) {
		if (inArray(curURI, needNormalUrl)) {
			gotoIndex();
			return false;
		}
	} else if (getUT()!=1) {
		if (inArray(curURI, needInnerUrl)) {
			gotoIndex();
			return false;
		}
	}
	if (getCT()!=1&&getCT()!=2) {
		if (inArray(curURI, needMonthUrl)) {
			gotoIndex();
			return false;
		}
	}	
	return true;
}

//统一处理导航的点击事件，主要是过滤登录状态。
function gotoSendDOrder() {
	delCookie("p");
	delCookie("p2");
    addCookie("p","_header_send");
    showSendHeader2();
	if (getL() == 0) {
		document.location.href = "/"+i18n.name+"/user/order_msg.html";
	} else {
		if (getUT() == 1) {
			document.location.href = "/"+i18n.name+ "/innerorder/inner_discount_order.html";
		} else {
			if('0' == isCustMem()) {
				document.location.href = "/"+i18n.name + "/order/Dorder.html";
			} else {
				document.location.href = "/"+i18n.name + "/order/batch_order.html";
			}
		}
	}
}

//统一处理导航的点击事件，主要是过滤登录状态。
		function gotoSendNormal() {
		delCookie("p");
		delCookie("p2");
    	addCookie("p","_header_send");
    	showSendHeader2();
	
	 
	if (getL() == 0) {
		document.location.href = "/"+i18n.name+"/user/order_msg.html";
	} else {
		if (getUT() == 1) {
			document.location.href = "/"+i18n.name+"/innerorder/inner_discount_order.html";
		} else {
			if('0' == isCustMem()) {
				document.location.href = "/"+i18n.name+"/order/order.html";
			} else {
				document.location.href = "/"+i18n.name+"/order/batch_order.html";
			}
		}
	}
}
function gotoSendMedicineOrder() {	
	if (getL() == 0) {
    	document.location.href = "/" + i18n.name + "/login.html?url=/" + i18n.name + "/order/order_medicine.html";
    } else {
    	delCookie("p");
    	delCookie("p2");
    	addCookie("p","_header_send");
    	showSendHeader2();
    	if (getUT() == 1) {
    		document.location.href = "/" + i18n.name + "/index.html";
    	} else {
    		document.location.href = "/" + i18n.name + "/order/order_medicine.html";
    	}
    }
}


function gotoTracingWaybill() {
	if (getL() == 0) {
		document.location.href = "/" + i18n.name + "/login.html?url=/" + i18n.name + "/waybill/tracing_waybill_query.html";
	} else {
		delCookie("p");
    	delCookie("p2");
    	addCookie("p","_header_tracing_waybill");
    	//showInfoSendHisHeader2();
    	if (getUT() == 0) {
    		addCookie("p2","_header2_tracing_waybill");
    		document.location.href = "/" + i18n.name + "/waybill/tracing_waybill_query.html";
    	} else {
    		document.location.href = "/" + i18n.name + "/index.html";
    	}
	}
}

function gotoInfoMyPoint() {
	if (getL() == 0) {
		document.location.href = "/" + i18n.name + "/login.html?url=/" + i18n.name + "/user/my_point.html";
	} else {
		delCookie("p");
    	delCookie("p2");
    	addCookie("p","_header_account_manager");
    	showAccountManagerHeader2();
    	if (getUT() == 0) {
    		addCookie("p2","_header2_info_my_point");
    		if(isCustMem() == 0){
        		document.location.href = "/" + i18n.name + "/user/my_point.html";
    		}else{
        		document.location.href = "/" + i18n.name + "/user/my_month_point.html";
    		}
    	} else {
    		document.location.href = "/" + i18n.name + "/index.html";
    	}
	}
}





//跳转权限管理页面
function gotoInfoAuthorization() {
	if (getL() == 0) {
		document.location.href = "/" + i18n.name + "/login.html?url=/" + i18n.name + "/user/authorization/authorization.html";
	} else {
		delCookie("p");
    	delCookie("p2");
    	addCookie("p","_header_account_manager");
    	if (getUT() == 0) {
    		addCookie("p2","_header2_info_authorization");
    		if(getCT() == "1"){
    			document.location.href = "/" + i18n.name + "/user/authorization/authorization.html";
    		} else {
    			document.location.href = "/" + i18n.name + "/index.html";
    		}
    	} else {
    		document.location.href = "/" + i18n.name + "/index.html";
    	}
	}
}


//$("#_header_info_advise1").click(function(){
//	if (getL() == 0) {
//		login();
//	} else {
//	    delCookie("p");
//	    addCookie("p","_header_info");
//		document.location.href = "/user/" + i18n.name + "/my_advise.html";
//	}
//});

$("#_header_home").click(function(){
    delCookie("p");
    addCookie("p","_header_home");
});




function gotoRegIndex(index){
	location.href=location.protocol+'//'+location.host+index;
}

function drawReportNav(){
	if(isGroupUser()){
		$("#_header_report_myreport_li").hide();
	}
	$("#_header_reprot").show();
}

//出口件-港澳台
function gotoExportHkmt(){	
	if (getL() == 0) {
    	document.location.href = "/" + commonUrl + "/login.html?url=/" + commonUrl + "/order/order_export.html";
    } else {
    	delCookie("p");
    	delCookie("p2");
    	addCookie("p","_header_send");
    	showSendHeader2();
    	if (getUT() == 1) {
    		document.location.href = "/" + commonUrl + "/index.html";
    	} else {
    		document.location.href = "/" + commonUrl + "/order/order_export.html";
    	}
    }
}

//出口件-国际
function gotoExportAbroad(){	
	if (getL() == 0) {
    	document.location.href = "/" + commonUrl + "/login.html?url=/" + commonUrl + "/order/order_export_abroad.html";
    } else {
    	delCookie("p");
    	delCookie("p2");
    	addCookie("p","_header_send");
    	showSendHeader2();
    	if (getUT() == 1) {
    		document.location.href = "/" + commonUrl + "/index.html";
    	} else {
    		document.location.href = "/" + commonUrl + "/order/order_export_abroad.html";
    	}
    }
}


hiddenLJJRSFHY();
/**隐藏“立即加入順豐會員”块*/
function hiddenLJJRSFHY(){
	if(getRegionCode()=='hk'){
		$('.action-tips.f14.mb20').parent().css("display","none");
	}
}

String.prototype.endWith=function(str){

    var reg=new RegExp(str+"$");

    return reg.test(this);

};
